/**
 * Copyright (c) 2018 开源 All rights reserved.
 * <p>
 * https://www.sxyc.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import io.renren.annotation.Login;
import io.renren.common.utils.Result;
import io.renren.modules.yckj.common.ThreadLocalUtil;
import io.renren.modules.yckj.dto.UserDTO;
import io.renren.modules.yckj.entity.UserEntity;
import io.renren.modules.yckj.enums.status.AuthenticationStatus;
import io.renren.modules.yckj.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 测试接口
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/yckj/user")
@Tag(name = "用户接口", description = "用户接口")
@AllArgsConstructor
public class ApiUserController {

    private final UserService userService;

    @Login
    @GetMapping("info")
    @Operation(summary = "获取信息")
    @ApiResponse(content = @Content(mediaType = "application/json", schema = @Schema(implementation = UserEntity.class)))
    public Result<UserEntity> info() {
        UserEntity userEntity = userService.getUserByUserId(ThreadLocalUtil.getUserId());
        return new Result<UserEntity>().ok(userEntity);
    }

    @Login
    @PostMapping("update")
    @Operation(summary = "完善信息")
    public Result<Boolean> update(@RequestBody UserDTO dto) {
        UserDTO user = new UserDTO();
        user.setId(ThreadLocalUtil.getUserId());
        user.setMobile(dto.getMobile());
        user.setUsername(dto.getUsername());
        user.setAvatar(dto.getAvatar());
        user.setCompany(dto.getCompany());
        user.setCreateDate(new Date());
        boolean b = userService.update(user);
        return new Result<Boolean>().ok(b);
    }

    @Login
    @GetMapping("byMobile")
    @Operation(summary = "根据手续号获取用户信息")
    @ApiResponse(content = @Content(mediaType = "application/json", schema = @Schema(implementation = UserEntity.class)))
    @Parameter(name = "mobile", description = "手机号", required = true)
    public Result<UserEntity> byMobile(@RequestParam("mobile") String mobile) {
        UserEntity userEntity = userService.getByMobile(mobile);
        return new Result<UserEntity>().ok(userEntity);
    }

    @Login
    @GetMapping("available/balance")
    @Operation(summary = "获取当前用户的可用金额")
    @ApiResponse(content = @Content(mediaType = "application/json", schema = @Schema(implementation = UserEntity.class)))
    @Parameter(name = "teamId", description = "组Id")
    public Result<Map<String, BigDecimal>> availableBalance(@RequestParam("teamId") Long teamId) {
        Map<String, BigDecimal> map = userService.getAvailableBalance(ThreadLocalUtil.getUserId(), teamId);
        return new Result<Map<String, BigDecimal>>().ok(map);
    }

    @Login
    @GetMapping("balance")
    @Operation(summary = "获取当前用户的余额")
    @ApiResponse(content = @Content(mediaType = "application/json", schema = @Schema(implementation = UserEntity.class)))
    @Parameter(name = "teamId", description = "组Id")
    public Result<Map<String, BigDecimal>> balance(@RequestParam("teamId") Long teamId) {
        Map<String, BigDecimal> map = userService.getBalance(ThreadLocalUtil.getUserId(), teamId);
        return new Result<Map<String, BigDecimal>>().ok(map);
    }

    @Login
    @GetMapping("idCardVerification")
    @Operation(summary = "实名认证当前用户")
    @ApiResponse(content = @Content(mediaType = "application/json"))
    @Parameter(name = "realName", description = "真实名称")
    @Parameter(name = "idNo", description = "卡号")
    @Parameter(name = "company", description = "学校/组织")
    public Result<Boolean> balance(@RequestParam("realName") String realName,
                                   @RequestParam("idNo") String idNo,
                                   @RequestParam("company") String company) {
        boolean b = userService.idCardVerification(realName, idNo);
        if (b) {
            UserEntity userEntity = new UserEntity();
            userEntity.setId(ThreadLocalUtil.getUserId());
            userEntity.setRealname(realName);
            userEntity.setIdNo(idNo);
            userEntity.setCompany(company);
            userEntity.setAuthentication(AuthenticationStatus.YES.getCode());
            b = userService.updateById(userEntity);
            return new Result<Boolean>().ok(b);
        } else {
            return new Result<Boolean>().error("身份证验证失败");
        }
    }


    @GetMapping("tags")
    @Operation(summary = "获取当前TAGS")
    @ApiResponse(content = @Content(mediaType = "application/json"))
    public Result<Object> tags() throws IOException {
        String json = userService.getTags();
        return new Result<>().ok(json);
    }


    @GetMapping("tag/uers")
    @Operation(summary = "获取当前TAG下的用户")
    @ApiResponse(content = @Content(mediaType = "application/json"))
    public Result<Object> tagUsers() throws IOException {
        String json = userService.getTagUsers();
        JSONObject jsonObject = new JSONObject(json);
        JSONArray openIds = jsonObject.getJSONObject("data").getJSONArray("openid");
//        for (Object openId : openIds) {
//            String msg = userService.sendTemplateMsg(openId.toString(), "机器1", "2025-04-17 00:00:00", "2025-04-17 08:00:00", "项目1", "13800000000");
//            System.out.printf("msg:%s\n", msg);
//        }

        return new Result<>().ok(json);
    }
}